/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.SetUtil;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AttrRight
/*     */   extends AdminRight
/*     */ {
/*  29 */   private Set<TargetType> mTargetTypes = new HashSet();
/*     */   private Set<String> mAttrs;
/*     */   
/*     */   AttrRight(String name, Right.RightType rightType) throws ServiceException
/*     */   {
/*  34 */     super(name, rightType);
/*     */     
/*  36 */     if ((rightType != Right.RightType.getAttrs) && (rightType != Right.RightType.setAttrs)) {
/*  37 */       throw ServiceException.FAILURE("internal error", null);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean isAttrRight() {
/*  42 */     return true;
/*     */   }
/*     */   
/*     */   public String dump(StringBuilder sb) {
/*  46 */     super.dump(sb);
/*     */     
/*  48 */     sb.append("===== attrs right properties: =====\n");
/*  49 */     sb.append("target types: ");
/*  50 */     for (TargetType tt : this.mTargetTypes)
/*  51 */       sb.append(tt.name() + " ");
/*  52 */     sb.append("\n");
/*     */     
/*  54 */     if (this.mAttrs == null) {
/*  55 */       sb.append("all attrs\n");
/*     */     } else {
/*  57 */       sb.append("attrs:\n");
/*  58 */       for (String a : this.mAttrs) {
/*  59 */         sb.append("    " + a + "\n");
/*     */       }
/*     */     }
/*     */     
/*  63 */     return sb.toString();
/*     */   }
/*     */   
/*     */   boolean overlaps(Right other) throws ServiceException
/*     */   {
/*  68 */     if (other.isPresetRight())
/*  69 */       return false;
/*  70 */     if (other.isAttrRight())
/*  71 */       return overlapAttrRight((AttrRight)other);
/*  72 */     if (other.isComboRight()) {
/*  73 */       ComboRight cr = (ComboRight)other;
/*  74 */       Set<AttrRight> otherAttrRights = cr.getAttrRights();
/*  75 */       for (AttrRight ar : otherAttrRights) {
/*  76 */         if (overlapAttrRight(ar)) {
/*  77 */           return true;
/*     */         }
/*     */       }
/*  80 */       return false;
/*     */     }
/*  82 */     throw ServiceException.FAILURE("internal error", null);
/*     */   }
/*     */   
/*     */   private boolean overlapAttrRight(AttrRight otherAttrRight) {
/*  86 */     if (this == otherAttrRight) {
/*  87 */       return true;
/*     */     }
/*  89 */     if (SetUtil.intersect(getTargetTypes(), otherAttrRight.getTargetTypes()).isEmpty()) {
/*  90 */       return false;
/*     */     }
/*  92 */     if ((allAttrs()) || (otherAttrRight.allAttrs())) {
/*  93 */       return true;
/*     */     }
/*     */     
/*  96 */     return !SetUtil.intersect(getAttrs(), otherAttrRight.getAttrs()).isEmpty();
/*     */   }
/*     */   
/*     */   boolean executableOnTargetType(TargetType targetType)
/*     */   {
/* 101 */     return this.mTargetTypes.contains(targetType);
/*     */   }
/*     */   
/*     */   boolean isValidTargetForCustomDynamicGroup()
/*     */   {
/* 106 */     return this.mTargetTypes.contains(TargetType.group);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean grantableOnTargetType(TargetType targetType)
/*     */   {
/* 114 */     for (TargetType tt : getTargetTypes()) {
/* 115 */       if (targetType.isInheritedBy(tt)) {
/* 116 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 120 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   Set<TargetType> getGrantableTargetTypes()
/*     */   {
/* 127 */     Set<TargetType> targetTypes = new HashSet();
/* 128 */     for (TargetType tt : getTargetTypes()) {
/* 129 */       SetUtil.union(targetTypes, tt.inheritFrom());
/*     */     }
/*     */     
/* 132 */     return targetTypes;
/*     */   }
/*     */   
/*     */   void setTargetType(TargetType targetType) throws ServiceException
/*     */   {
/* 137 */     this.mTargetTypes.add(targetType);
/*     */   }
/*     */   
/*     */   void verifyTargetType()
/*     */     throws ServiceException
/*     */   {}
/*     */   
/*     */   public TargetType getTargetType() throws ServiceException
/*     */   {
/* 146 */     throw ServiceException.FAILURE("internal error", null);
/*     */   }
/*     */   
/*     */   public Set<TargetType> getTargetTypes() {
/* 150 */     return this.mTargetTypes;
/*     */   }
/*     */   
/*     */ 
/*     */   public String getTargetTypeStr()
/*     */   {
/* 156 */     StringBuilder sb = new StringBuilder();
/* 157 */     boolean first = true;
/* 158 */     for (TargetType tt : this.mTargetTypes) {
/* 159 */       if (first) {
/* 160 */         first = false;
/*     */       } else {
/* 162 */         sb.append(",");
/*     */       }
/* 164 */       sb.append(tt.getCode());
/*     */     }
/* 166 */     return sb.toString();
/*     */   }
/*     */   
/*     */   void addAttr(String attrName) throws ServiceException {
/* 170 */     if (getRightType() == Right.RightType.setAttrs) {
/* 171 */       HardRules.checkForbiddenAttr(attrName);
/*     */     }
/*     */     
/* 174 */     if (this.mAttrs == null) {
/* 175 */       this.mAttrs = new HashSet();
/*     */     }
/*     */     
/* 178 */     this.mAttrs.add(attrName);
/*     */   }
/*     */   
/*     */   public boolean allAttrs() {
/* 182 */     return this.mAttrs == null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public Set<String> getAttrs()
/*     */   {
/* 189 */     return this.mAttrs;
/*     */   }
/*     */   
/*     */   Set<String> getAllAttrs() throws ServiceException {
/* 193 */     if (!allAttrs()) {
/* 194 */       throw ServiceException.FAILURE("internal error, can only be called if allAttrs is true", null);
/*     */     }
/*     */     
/*     */ 
/* 198 */     if (this.mTargetTypes.size() != 1) {
/* 199 */       throw ServiceException.FAILURE("internal error", null);
/*     */     }
/*     */     
/* 202 */     TargetType tt = (TargetType)this.mTargetTypes.iterator().next();
/* 203 */     return AttributeManager.getInstance().getAllAttrsInClass(tt.getAttributeClass());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean suitableFor(Right.RightType needed)
/*     */   {
/* 219 */     if ((needed == this.mRightType) || ((needed == Right.RightType.getAttrs) && (this.mRightType == Right.RightType.setAttrs)))
/*     */     {
/* 221 */       return true;
/*     */     }
/* 223 */     return false;
/*     */   }
/*     */   
/*     */   void completeRight()
/*     */     throws ServiceException
/*     */   {
/* 229 */     super.completeRight();
/*     */     
/* 231 */     if (this.mTargetTypes.size() == 0) {
/* 232 */       throw ServiceException.PARSE_ERROR("missing target type", null);
/*     */     }
/*     */     
/*     */ 
/* 236 */     if ((allAttrs() == true) && (this.mTargetTypes.size() != 1)) {
/* 237 */       throw ServiceException.PARSE_ERROR("there must be exactly one target type for getAttrs/setAttrs right that cover all attributes", null);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void validateAttr(String attrName)
/*     */     throws ServiceException
/*     */   {
/* 250 */     AttributeManager am = AttributeManager.getInstance();
/* 251 */     for (TargetType tt : this.mTargetTypes) {
/* 252 */       AttributeClass klass = tt.getAttributeClass();
/* 253 */       if (!am.getAllAttrsInClass(klass).contains(attrName)) {
/* 254 */         throw ServiceException.FAILURE("attribute " + attrName + " is not on " + tt.getCode(), null);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/AttrRight.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */